//
// Created by kqbi on 2020/2/26.
//

#include <json/json.h>
#include "HttpService.h"
#include "DBS/Store.h"

HttpService::HttpService(std::weak_ptr<Store> store) : _store(store) {

}

HttpService::~HttpService() {

}

void
HttpService::VehicleServiceSearch(std::string connectionId, VehicleService_Search &vSearch, ResultHandle resultHandle) {
    auto strongSelf = _store.lock();
    int sum = 0;
    std::vector<AbstractDb::PlateInfoRecord> recordList;
    if (strongSelf) {
        strongSelf->QueryPlateInfoList(sum, (vSearch.page - 1) * vSearch.pageSize, vSearch.pageSize, vSearch._condition.beginTime,
                                       vSearch._condition.endTime, recordList);
    }
    Json::Value rootValue;
    Json::StyledWriter writer;
    rootValue["totalCount"] = (int) recordList.size();
    for (std::vector<AbstractDb::PlateInfoRecord>::iterator it = recordList.begin();
         it != recordList.end(); ++it) {
        Json::Value v;
        v["id"] = (*it)._iD;
        v["platename"] = (*it)._plateName;
        v["platetype"] = (*it)._plateType;
        v["confidence"] = (*it)._confidence;
        v["fullpath"] = (*it)._fullPath;
        v["platepath"] = (*it)._platePath;
        v["createtime"] = (*it)._createTime;
        rootValue["results"].append(v);
    }
    std::string result = writer.write(rootValue);
    resultHandle(connectionId, (VIICS_status)0, result);
}